<template>
  <div class="login-container">
    <div class="login-content">
      <h1 class="title">校园智慧竞赛平台</h1>
      
      <div class="login-form">
        <div class="form-item">
          <label>学号：</label>
          <input 
            v-model="loginForm.studentId" 
            type="text" 
            class="form-input"
            placeholder="请输入学号"
          />
        </div>
        
        <div class="form-item">
          <label>密码：</label>
          <input 
            v-model="loginForm.password" 
            type="password" 
            class="form-input"
            placeholder="请输入密码"
          />
        </div>
        
        <div class="button-group">
          <button class="register-btn" @click="handleRegister">注册</button>
          <button class="login-btn" @click="handleLogin">登录</button>
        </div>
      </div>
      
      <div class="forgot-password">
        <a href="#" @click.prevent="handleForgotPassword">忘记密码?</a>
      </div>
    </div>

    <!-- 自定义弹窗 -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ modalTitle }}</h3>
          <button class="close-btn" @click="closeModal">×</button>
        </div>
        <div class="modal-body">
          <div class="icon-wrapper">
            <div class="info-icon">!</div>
          </div>
          <p class="modal-text">{{ modalText }}</p>
          <p v-if="modalContactInfo" class="contact-info">{{ modalContactInfo }}</p>
        </div>
        <div class="modal-footer">
          <button class="confirm-btn" @click="closeModal">我知道了</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { studentLoginService, getStudentInfoService } from '@/api/student.js'
import { useTokenStore } from '@/stores/token.js'
import { useStudentStore } from '@/stores/student.js'

const router = useRouter()
const tokenStore = useTokenStore()
const studentStore = useStudentStore()

// 表单数据
const loginForm = reactive({
  studentId: '',
  password: ''
})

// 弹窗控制
const showModal = ref(false)
const modalTitle = ref('')
const modalText = ref('')
const modalContactInfo = ref('')

// 显示弹窗
const showCustomModal = (title, text, contactInfo = '') => {
  modalTitle.value = title
  modalText.value = text
  modalContactInfo.value = contactInfo
  showModal.value = true
}

// 登录处理
const handleLogin = async () => {
  if (!loginForm.studentId || !loginForm.password) {
    showCustomModal('提示', '请输入学号和密码')
    return
  }
  
  try {
    // 调用登录接口
    const result = await studentLoginService(loginForm)
    
    if (result.code !== 200) {
      showCustomModal('登录失败', result.msg || '登录失败，请检查学号和密码')
      return
    }
    
    // 根据审核状态处理跳转
    const auditStatus = result.data.auditStatus
    if (auditStatus === 0) {
      showCustomModal('提示', '您的账号正在审核中，审核通过后方可使用系统功能', '请耐心等待管理员审核')
      return
    } else if (auditStatus === 2) {
      showCustomModal('提示', '您的账号审核未通过，请联系管理员', '如有疑问请联系相关负责人')
      return
    }

    // 只有审核通过才存储token
    if (result.data.token && result.data.token.trim() !== '') {
      console.log('存储token:', result.data.token)
      tokenStore.setToken(result.data.token)

      // 获取学生详细信息
      try {
        console.log('开始获取学生信息...')
        const studentInfoResult = await getStudentInfoService()
        console.log('获取学生信息结果:', studentInfoResult)
        if (studentInfoResult.code === 200) {
          studentStore.setStudentInfo(studentInfoResult.data)
          console.log('学生信息已存储到store')
        }
      } catch (error) {
        console.error('获取学生信息失败:', error)
        // 即使获取学生信息失败，也允许跳转到主页
      }
    } else {
      showCustomModal('登录失败', '登录验证失败，请重试')
      return
    }
    
    // 审核通过，跳转到主页
    showCustomModal('登录成功', '欢迎进入校园智慧竞赛平台')
    setTimeout(() => {
      router.push('/home')
    }, 1500)
    
  } catch (error) {
    console.error('登录失败:', error)
    showCustomModal('登录失败', error.msg || '登录失败，请检查网络连接')
  }
}

// 注册处理
const handleRegister = () => {
  router.push('/register')
}

// 忘记密码处理
const handleForgotPassword = () => {
  showCustomModal('密码重置', '请联系管理员重置密码', '联系电话：135****5111')
}

// 关闭弹窗
const closeModal = () => {
  showModal.value = false
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}

.login-content {
  width: 100%;
  max-width: 350px;
  text-align: center;
}

.title {
  font-family: 'Microsoft YaHei', sans-serif;
  color: white;
  font-size: 36px;
  font-weight: bolder;
  margin-top: 200px;
  margin-bottom: 180px;
  letter-spacing: 2px;
}

.login-form {
  background: white;
  border-radius: 15px;
  padding: 40px 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  margin-bottom: 100px;
}

.form-item {
  margin-bottom: 25px;
  display: flex;
  align-items: center;
  gap: 15px;
}

.form-item label {
  color: #333;
  font-size: 16px;
  font-weight: bolder;
  min-width: 50px;
  text-align: left;
}

.form-input {
  width: 100%;
  height: 45px;
  border: none;
  border-bottom: 2px solid #e0e0e0;
  background: transparent;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  border-bottom-color: #667eea;
}

.form-input::placeholder {
  color: #999;
}

.button-group {
  display: flex;
  gap: 15px;
  margin-top: 35px;
}

.register-btn,
.login-btn {
  flex: 1;
  height: 45px;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-btn {
  background: white;
  color: #667eea;
  border: 2px solid #667eea;
}

.register-btn:hover {
  background: #667eea;
  color: white;
}

.login-btn {
  background: linear-gradient(135deg, #667eea 0%, #4b51a2 100%);
  color: white;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.forgot-password {
  text-align: center;
}

.forgot-password a {
  color: white;
  font-size: 18px;
  text-decoration: none;
  opacity: 0.8;
  transition: opacity 0.3s ease;
}

.forgot-password a:hover {
  opacity: 1;
}

/* 移动端自适应 */
@media (max-width: 480px) {
  .login-container {
    padding: 15px;
  }
  
  .title {
    font-size: 20px;
    margin-bottom: 40px;
    margin-top: 80px;
  }
  
  .login-form {
    padding: 30px 20px;
    border-radius: 12px;
    margin-bottom: 40px;
  }
  
  .form-item {
    gap: 10px;
  }
  
  .form-item label {
    min-width: 45px;
    font-size: 14px;
  }
  
  .form-input {
    height: 40px;
    font-size: 14px;
  }
  
  .register-btn,
  .login-btn {
    height: 40px;
    font-size: 14px;
  }
  
  .button-group {
    gap: 10px;
  }
  
  .forgot-password a {
    font-size: 14px;
  }
}

@media (max-width: 375px) {
  .login-form {
    padding: 25px 15px;
  }
  
  .title {
    font-size: 18px;
    margin-top: 60px;
    margin-bottom: 80px;
  }
  
  .form-item {
    margin-bottom: 20px;
    gap: 8px;
  }
  
  .form-item label {
    min-width: 40px;
    font-size: 13px;
  }
  
  .button-group {
    margin-top: 25px;
  }
}

/* 横屏适配 */
@media (max-height: 600px) and (orientation: landscape) {
  .login-container {
    padding: 10px;
  }
  
  .title {
    margin-bottom: 30px;
    font-size: 20px;
  }
  
  .login-form {
    padding: 25px 30px;
  }
  
  .form-item {
    margin-bottom: 20px;
  }
  
  .button-group {
    margin-top: 25px;
  }
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease-out;
}

.modal-content {
  background: white;
  border-radius: 20px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.3s ease-out;
  position: relative;
  overflow: hidden;
}

.modal-header {
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  color: white;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.3s ease;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-body {
  padding: 30px 20px;
  text-align: center;
}

.icon-wrapper {
  margin-bottom: 20px;
}

.info-icon {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 30px;
  font-weight: bold;
  margin: 0 auto;
  animation: pulse 2s infinite;
}

.modal-text {
  font-size: 16px;
  color: #333;
  margin: 0 0 10px 0;
  font-weight: 500;
}

.contact-info {
  font-size: 18px;
  color: #667eea;
  margin: 0;
  font-weight: 600;
  letter-spacing: 1px;
}

.modal-footer {
  padding: 0 20px 20px;
  text-align: center;
}

.confirm-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 120px;
}

.confirm-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: scale(0.7);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

/* 弹窗移动端适配 */
@media (max-width: 480px) {
  .modal-content {
    width: 95%;
    margin: 20px;
  }
  
  .modal-header {
    padding: 15px;
  }
  
  .modal-header h3 {
    font-size: 16px;
  }
  
  .modal-body {
    padding: 25px 15px;
  }
  
  .info-icon {
    width: 50px;
    height: 50px;
    font-size: 24px;
  }
  
  .modal-text {
    font-size: 14px;
  }
  
  .contact-info {
    font-size: 16px;
  }
  
  .confirm-btn {
    padding: 10px 25px;
    font-size: 14px;
  }
}
</style>
